package com.gxa.controller.commserve;

import com.gxa.common.utils.Result;
import com.gxa.dto.commservedto.LostFoundSearchCriteria;
import com.gxa.entity.commserve.LostFound;
import com.gxa.service.commserve.LostFoundService;
import com.gxa.service.commserve.RecruitmentRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:18:00
 */
@Api(tags = "失物招领")
@RestController
@RequestMapping("/lost-found")
public class LostFoundController {
    @Autowired
    private LostFoundService lostFoundService;
    @Autowired
    private RecruitmentRecordService recruitmentRecordService;

//    List<LostFound> lostFoundList;
//    Date date = new Date("2022/10/11");
//    private List<LostFound> list;
//    public LostFoundController(){
//        this.init();
//    }
//    void init(){
//        this.lostFoundList = new ArrayList<>();
//
//        LostFound lostFound = new LostFound(1,"zs","sadfss",date,date,"李四","12345678910","成都",0,0);
//        this.lostFoundList.add(lostFound);
//        LostFound lostFound1 = new LostFound(2,"zs","sadfss",date,date,"李四","12345678910","成都",0,0);
//        this.lostFoundList.add(lostFound1);
//    }



    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询失物招领")
    @GetMapping("list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
       try{
           Map<String, Object> map = this.lostFoundService.queryList(pageNum,pageSize);
           return Result.success(map);
       }catch (Exception e){
           e.printStackTrace();
           return Result.failed("查询失败");
       }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @PostMapping("search")
    @ApiOperation(value = "搜索失物招领")
    public Result search(@RequestBody LostFoundSearchCriteria lostFoundSearchCriteria, Integer pageNum, Integer pageSize){
       try {
           Map<String, Object> map = this.lostFoundService.queryListBySearch(lostFoundSearchCriteria, pageNum, pageSize);
           return Result.success(map);
       }catch (Exception e){
           e.printStackTrace();
           return Result.failed("搜索失败");
       }
    }


    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询当前用户报修")
    @GetMapping("queryById/{id}")
    public Result preEdit(@PathVariable("id") Integer id){
       try {
           LostFound lostFound = this.lostFoundService.queryById(id);
           return Result.success(lostFound);
       }catch (Exception e){
           e.printStackTrace();
           return Result.failed("当前用户查询失败");
       }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "修改失物招领状态")
    @PutMapping("edit")
    public  Result edit (@RequestBody LostFound lostFound){
       try{
           this.lostFoundService.updateState(lostFound);
           Result<Object> result = Result.success("修改成功");
           return result;
       }catch (Exception e){
           e.printStackTrace();
           return Result.failed("修改失败");
       }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "发布失物招领")
    @PostMapping("add")
//    @RequestBody LostFound lostFound,
 public Result add (@RequestBody LostFound lostFound){
        try{

            this.lostFoundService.addLostFound(lostFound);

            Result<Object> result = Result.success("添加成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败");
        }
    }


}
